#!/usr/bin/python
# -*- coding: utf-8 -*-

"""Project Euler Solution 071

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import math
import cProfile
from fractions import gcd

def get_answer():
	"""Question:
	
	Consider the fraction, n/d, where n and d are positive integers. If n<d 
	and HCF(n,d)=1, it is called a reduced proper fraction.

	If we list the set of reduced proper fractions for d ≤ 8 in ascending 
	order of size, we get:
	
	1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/5, 5/8, 2/3,
	5/7, 3/4, 4/5, 5/6, 6/7, 7/8
	
	It can be seen that 2/5 is the fraction immediately to the left of 3/7.
	
	By listing the set of reduced proper fractions for d ≤ 1,000,000 in 
	ascending order of size, find the numerator of the fraction immediately to 
	the left of 3/7.
	"""
	#The numerator of the highest fraction with the denominator equal to 
	#prev_denominator.
	prev_high_numerator = 2
	
	#The previous denominator tested.
	prev_denominator = 5
		
	#The numerator of the fraction which the answer is to the left of.
	highest_numerator = 3
	
	#The denominator of the fraction which the answer is to the left of.
	highest_denominator = 7
	
	#The new denominator which will be tested.
	curr_denominator = 6
	
	#Go through all the denominators less than or equal to 1,000,000. 
	#For each denominator test all the possible numerators until the 
	#highest proper fraction lower than 3/7 and higher than the highest 
	#proper fraction of the previous denominator is found.
	while curr_denominator <= 1000000:				
		#The highest possible numerator which can be tested for this 
		#denominator. 
		#i.e. the numerator of 3/7 with this denominator as its base, - 1.
		numerator_to = int(
							(float(highest_numerator) * curr_denominator) 
								/ highest_denominator
							) - 1
		
		#The lowest possible numerator which can be tested for this 
		#denominator.
		#i.e. the numerator of the highest proper fraction for the previous 
		#denominator, with this denominator as its base.
		numerator_from = int(
							math.ceil(
									(float(prev_high_numerator) 
										* curr_denominator) 
									/ prev_denominator
								)
						)
		
		#Find the numerator of the highest proper fraction for 
		#this denominator. 
		for n in xrange(numerator_to, numerator_from - 1, -1):
			if gcd(n, curr_denominator) == 1:
				prev_high_numerator = n
				prev_denominator = curr_denominator
			
				break
		
		#Move to the next denominator.
		curr_denominator += 1
		
	#Return the result.
	return prev_high_numerator
	
if __name__ == "__main__":
	cProfile.run("print(get_answer())")
